<?php
/*
用作代理平台余额操作
*/
class Site_balance_logModel extends Model
{
	public $error = '';
    //列表
    public function _list($view, $map, $sort = 'sort', $pagesize = 20)
    {
        //计算总页数
        $count = $this->where($map)->count();
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->where($map)->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->select();
        $pages = $p->show();
        $view->assign('list', $list);
        $view->assign('pages', $pages);

    }

    //改变平台余额
    public function change($num = 0, $site_id = 0, $remark = '', $user_id = -1)
    {
        if ($num == 0) {
			$this->error = '金额无变动！';
            return false;
        }
		$siteDB = M('Site');
		$money = $siteDB -> where(array('id' => $site_id)) -> getField('balance');
		
		if(abs($num*1) > $money && $num*1 < 0){
			$this->error = '平台余额不足，扣款失败！';
			return false;
		}
		
        $data['user_id'] = $user_id == -1 ? $_SESSION[C('USER_AUTH_KEY')] : $user_id;
        $data['remark'] = $remark;
		$data['site_id'] = $site_id;
        $data['changenum'] = $num;
        $data['dateline'] = time();
		
        $id = $this->add($data);
		 
		if($id){
            $ret = $siteDB->where(array('id' => $site_id))->setInc('balance', $num);
			if($ret !== false){
				return true;
			}else{
				$this->where(array('id' => $id)) -> delete();
				return false;
			}
		}else{
			return false;
		}
    }



}
